﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SpringStudio.Common;
using SpringStudio.DAL;

namespace Sdepci.InfoCode.BLL.Authority
{
    /// <summary>
    /// 角色管理器类
    /// </summary>
    public class RoleManager
    {
        /// <summary>
        /// 设置默认角色
        /// </summary>
        /// <param name="roleGuid">角色Guid</param>
        /// <returns>是否成功</returns>
        public bool SetDefaultRole(string roleGuid)
        {
            try
            {
                string sql = string.Empty;

                // 先把其余设置为非默认
                sql = "Update InfoCodeRoles Set DefaultRole=@DefaultRole";
                SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@DefaultRole", false));

                // 再把已选项设置为默认
                sql = "Update InfoCodeRoles Set DefaultRole=@DefaultRole Where RoleGuid=@RoleGuid";
                SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@RoleGuid", roleGuid),
                    SqlHelper.NewSqlParameter("@DefaultRole", true));

                return true;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 获取默认角色
        /// </summary>
        /// <returns></returns>
        public Role GetDefaultRole()
        {
            try
            {
                string sql = string.Empty;
                sql = "Select RoleGuid,RoleName,FuncGuids,DefaultRole,Seq From InfoCodeRoles Where DefaultRole=@DefaultRole";
                DataSet ds = SqlHelper.ExecuteDataSet(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@DefaultRole", true));
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    return new Role(row["RoleGuid"].ToString(), row["RoleName"].ToString(), row["FuncGuids"].ToString(), (bool)row["DefaultRole"], int.Parse(row["Seq"].ToString()));
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return null;
            }
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="roleName">角色名称</param>
        /// <param name="funcGuids">角色权限</param>
        /// <param name="seq">显示次序</param>
        /// <returns>是否成功</returns>
        public bool Add(string roleName, string funcGuids, int seq)
        {
            try
            {
                string sql = string.Empty;
                string roleGuid = Guid.NewGuid().ToString(); // 创建一个新的Guid
                sql = "Insert Into InfoCodeRoles (RoleGuid,RoleName,FuncGuids,DefaultRole,Seq) Values(@RoleGuid,@RoleName,@FuncGuids,@DefaultRole,@Seq)";
                SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@RoleGuid", roleGuid),
                    SqlHelper.NewSqlParameter("@RoleName", roleName),
                    SqlHelper.NewSqlParameter("@FuncGuids", funcGuids),
                    SqlHelper.NewSqlParameter("@DefaultRole", false),
                    SqlHelper.NewSqlParameter("@Seq", seq));

                return true;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="roleGuid">角色Guid</param>
        /// <returns>是否成功</returns>
        public bool Delete(string roleGuid)
        {
            try
            {
                string sql = string.Empty;
                sql = "Delete From InfoCodeRoles Where RoleGuid=@RoleGuid";
                SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@RoleGuid", roleGuid));

                return true;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="roleGuid">角色Guid</param>
        /// <param name="roleName">角色名称</param>
        /// <param name="funcGuids">角色权限</param>
        /// <param name="seq">显示次序</param>
        /// <returns>是否成功</returns>
        public bool Update(string roleGuid, string roleName, string funcGuids, int seq)
        {
            try
            {
                string sql = string.Empty;
                sql = "Update InfoCodeRoles Set RoleName=@RoleName,FuncGuids=@FuncGuids,Seq=@Seq Where RoleGuid=@RoleGuid";
                SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@RoleGuid", roleGuid),
                    SqlHelper.NewSqlParameter("@RoleName", roleName),
                    SqlHelper.NewSqlParameter("@FuncGuids", funcGuids),
                    SqlHelper.NewSqlParameter("@Seq", seq));

                return true;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 获取角色列表（数据集）
        /// </summary>
        /// <returns></returns>
        public DataSet GetRoleDs()
        {
            try
            {
                string sql = string.Empty;
                sql = "Select RoleGuid,RoleName,FuncGuids,DefaultRole,Seq From InfoCodeRoles Order by Seq Asc";
                return SqlHelper.ExecuteDataSet(ConnectionString.Default, CommandType.Text, sql);
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return null;
            }
        }

        /// <summary>
        /// 获取角色列表（数组）
        /// </summary>
        /// <returns></returns>
        public List<Role> GetRoleList()
        {
            try
            {
                List<Role> list = new List<Role>();

                DataSet ds = this.GetRoleDs();
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        list.Add(new Role(row["RoleGuid"].ToString(), row["RoleName"].ToString(), row["FuncGuids"].ToString(), (bool)row["DefaultRole"], int.Parse(row["Seq"].ToString())));
                    }
                }

                return list;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return null;
            }
        }

        /// <summary>
        /// 获取角色字典（键值对）
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetRoleDict()
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();

            List<Role> list = this.GetRoleList();
            foreach (Role role in list)
            {
                dict.Add(role.RoleGuid, role.RoleName);
            }

            return dict;
        }
    }
}
